@extends('admin.layouts.header_other')
@section('title',"后台管理系统")
@section('content')
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="">
        <div class="layui-input-item">
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm padding_top" lay-event="adminUserAdd">添加</button>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm padding_top" lay-event="adminUserRefresh">刷新</button>
            </div>
            <div  class="layui-inline">
                <input type="text" name="name" id="name" placeholder="请输入用户" autocomplete="on" class="layui-input serach_input layui-btn-sm ">
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm padding_top" lay-event="adminUserSearch">搜索</button>
            </div>
        </div>

    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="{{asset('attach/js/layui.all.js')}}"></script>
<script>
    layui.use(['table','jquery'], function(){
        var table = layui.table,
            $ = layui.$;
        table.render({
            elem: '#test'
            ,url:'{{route('admin.admin.getlistdata')}}'
            ,toolbar: '#toolbarDemo'
            ,title: '后台用户管理'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'用户名',width:120,sort:true}
                ,{field:'email', title:'邮箱', width:180}
                ,{field:'mobile', title:'手机号', width:260, sort: true}
                ,{field:'sex', title:'性别', width:80, sort: true,templet:function (d) {
                   if(d.sex==1){
                       return '男';
                   }else{
                       return '女';
                   }
                 }}
                ,{field:'login_total', title:'登陆次数', width:100,sort:true}
                ,{field:'login_ip', title:'登录IP',width:180,sort:true}
                ,{field:'status', title:'用户状态', width:105, templet:function (d) {
                    var str='';
                     str = "<input type='checkbox' name='sex' value='"+d.status+"' lay-skin='switch' lay-text='屏蔽|开启' lay-filter='' ";
                           if(d.status==1){
                              str+="checked";
                           }
                         str+=">";
                           return str;
                }}
                ,{field:'created_at', title:'创建时间',width:200}
                ,{field:'updated_at', title:'更新时间',width:200}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
            ,loading:true
            ,size:'lg'
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            console.log(obj);
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'adminUserRefresh':
                    location.reload();
                    break;
                case 'adminUserAdd':
                    var openwin=layer.open({
                        type:2,
                        title:"后台用户添加",
                        content:'{{route('admin.admin.add')}}',
                        area:['100%','100%'],
                        maxmin:true,
                    });
                    layer.full(openwin);
                    break;
                case 'adminUserSearch':
                    $.ajax({
                       type:'get',
                       data:{name:$("#name").val()},
                       contentType: "application/json",
                       url:"{{route('admin.admin.getlistdata')}}",
                       success:function (res) {
                           if(res.code=0){
                               layer.msg(data.msg,{
                                   icon:6,
                                   time:2000
                               },function () {
                                   location.reload();//刷新父页面
                               });
                           }else{
                               layer.msg(data.msg,{icon:5});
                               return;
                           }
                       }

                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
@endsection
